package _17_剑指Offer;


import _17_剑指Offer.datastrcute.ListNode;

import java.util.ArrayList;
import java.util.List;

public class _06_剑指Offer从尾到头打印链表 {

    private int size;

    // 迭代思路，向获取元素的size，然后迭代逆序添加元素即可

    // 直接返回int数组
    public int[] reversePrint(ListNode head) {
        return ergodic(head, 0);
    }

    public int[] ergodic(ListNode head, int index) {
        if (head == null) {
            size = index - 1;
            System.out.println(size);
            return new int[size];
        }

        int[] list = ergodic(head.next, index + 1);
        list[size - index] = head.val;
        return list;
    }

    // 直接用List数组
    public int[] reversePrint1(ListNode head) {
        List<Integer> results = ergodic1(head);
        int len = results.size();
        int[] ins = new int[len];
        for (int i = 0; i < len; i++) {
            ins[i] = results.get(i);
        }
        return ins;
    }


    public List<Integer> ergodic1(ListNode head) {
        if (head == null) {
            return new ArrayList<>();
        }

        List<Integer> list = ergodic1(head.next);
        list.add(head.val);
        return list;
    }

}
